//
//  OIQRScanResultViewController.m
//  EducationOID
//
//  Created by slz on 2017/5/10.
//  Copyright © 2017年 bnsr. All rights reserved.
//

#import "OIQRScanResultViewController.h"
#import "NewWebViewController.h"

@interface QRScanResultTopView : UIView
@property (nonatomic,strong) UIImageView *targetResultImg;
@property (nonatomic,strong) UILabel *targetResultLabel;
@property(nonatomic,assign) BOOL result;

@end

NSString * kScanResultSuccess = @"此二维码经\"教育OID注册解析中心\"认证通过，为安全二维码。";
NSString * kScanResultFailed = @"此二维码未经\"教育OID注册解析中心\"认证通过，存在安全风险，请谨慎访问！";



@implementation QRScanResultTopView

-(instancetype)initWithScanResult:(BOOL)result{
    self = [super init];
    if (self){
        _result = result;
        _targetResultImg = [UIImageView new];
        [self addSubview:_targetResultImg];
        [_targetResultImg mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(self.mas_centerY);
            make.size.mas_equalTo(CGSizeMake(32, 32));
            make.left.mas_equalTo(self.mas_left).offset(12);
        }];
        _targetResultImg.image = [UIImage imageNamed:result ? @"scan_result_success" : @"scan_result_failed"];
        
        _targetResultLabel = [UILabel new];
        _targetResultLabel.fontSize = 15;
        _targetResultLabel.textColor = [UIColor whiteColor];
        [self addSubview:_targetResultLabel];
        [_targetResultLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(_targetResultImg.mas_right).offset(12);
            make.right.mas_equalTo(self.mas_right).offset(-12);
            make.centerY.mas_equalTo(_targetResultImg.mas_centerY);
        }];
        _targetResultLabel.text = result ? kScanResultSuccess : kScanResultFailed;
       
        _targetResultLabel.numberOfLines = 0;
        
        UIView * lineView = [UIView new];
        [self addSubview:lineView];
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.and.bottom.mas_equalTo(self);
            make.height.mas_equalTo(0.5);
        }];
        lineView.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

@end


@interface OIQRScanResultViewController ()
@property (nonatomic,strong) QRScanResultTopView *topView;
@property (nonatomic,strong) UILabel *linkLabel;
@end

@implementation OIQRScanResultViewController
{
    UIButton * cancleBtn;
    UIButton * sureBtn;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.showNavigationBar = YES;
    self.view.backgroundColor = kBack_Color_Default;
    [self setControllerTitle:@"扫描结果"];
    UIBarButtonItem * item = [UIBarButtonItem itemWithImage:@"top_back_1" selectImage:nil addTarget:self andAction:@selector(backAction)];
    self.navigationItem.leftBarButtonItem = item;

    UIBarButtonItem * collectItem = [UIBarButtonItem itemWithImage:@"icon_collect_white_blank" selectImage:nil addTarget:self andAction:@selector(collectionAction)];
    self.navigationItem.rightBarButtonItem = collectItem;
    
    _topView = [[QRScanResultTopView alloc]initWithScanResult:_isOidValid];
    [self.view addSubview:_topView];
    [_topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.and.top.mas_equalTo(self.view);
        make.height.mas_equalTo(80);
    }];
    if (_topView.result)
    {
        if (self.resultName.length > 0){
            _topView.targetResultLabel.text = [NSString stringWithFormat:@"此二维码经\"教育OID注册解析中心\"认证通过，为\"%@\"二维码。",self.resultName];
        }else
        {
            _topView.targetResultLabel.text = kScanResultSuccess;
        }
    }else{
        _topView.targetResultLabel.text = kScanResultFailed;
    }
    
    _nameLabel = [UILabel new];
    _nameLabel.fontSize = 16;
    _nameLabel.textAlignment = NSTextAlignmentCenter;
    _nameLabel.textColor = [UIColor whiteColor];
    _nameLabel.numberOfLines = 0;
    [self.view addSubview:_nameLabel];
    [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.view.mas_left).offset(12);
        make.right.mas_equalTo(self.view.mas_right).offset(-12);
        make.top.mas_equalTo(_topView.mas_bottom).offset(40);
    }];
    
    _linkLabel = [UILabel new];
    _linkLabel.fontSize = 14;
    _linkLabel.text = _isQrcode ? _resultString : _resultSuccessUrl;
    _linkLabel.textColor = [UIColor whiteColor];
    _linkLabel.textAlignment = NSTextAlignmentCenter;
    _linkLabel.numberOfLines = 0;
    [self.view addSubview:_linkLabel];
    [_linkLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.view.mas_left).offset(12);
        make.right.mas_equalTo(self.view.mas_right).offset(-12);
        make.top.mas_equalTo(_nameLabel.mas_bottom).offset(20);
    }];

    if (_canJump){
        cancleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [cancleBtn setTitle:@"取消" forState:UIControlStateNormal];
        [cancleBtn setTitleColor:kDark_TextColor_Default forState:UIControlStateNormal];
        cancleBtn.titleLabel.fontSize = 14;
        cancleBtn.backgroundColor = [UIColor whiteColor];
        cancleBtn.layer.cornerRadius = 3;
        [cancleBtn addTarget:self action:@selector(cancleAction) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:cancleBtn];
        [cancleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(60, 30));
            make.top.mas_equalTo(_linkLabel.mas_bottom).offset(30);
            make.right.mas_equalTo(self.view.mas_right).offset(-50);
        }];
        
        sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [sureBtn setTitle:@"继续" forState:UIControlStateNormal];
        [sureBtn setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
        sureBtn.backgroundColor = [UIColor whiteColor];
        sureBtn.layer.cornerRadius= 3;
        [sureBtn addTarget:self action:@selector(continueAction) forControlEvents:UIControlEventTouchUpInside];
        sureBtn.titleLabel.fontSize = 14;
        [self.view addSubview:sureBtn];
        [sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(60, 30));
            make.top.mas_equalTo(_linkLabel.mas_bottom).offset(30);
            make.right.mas_equalTo(self.view.mas_left).offset(110);
        }];
    }else
    {
        if (_typeCount == 2)
        _nameLabel.text = @"请前往微信客户端浏览";
    }
   
    // Do any additional setup after loading the view.
}

#pragma MARK - Action

- (void)continueAction{
    NewWebViewController * webController = [[NewWebViewController alloc]init];
    webController.url = _isQrcode ? _resultString : _resultSuccessUrl;
    [self.navigationController pushViewController:webController animated:YES];
}

- (void)cancleAction{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)backAction{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)collectionAction{
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
